
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#define BOOT_SIZE                       0x4000

int main(int argc, char **argv)
{
    uint8_t tmpBuffer[BOOT_SIZE] = {0};
    uint32_t readLen = 0;
    uint32_t objectLen = 0;

    /* file msg */
    char objectFileName[20] = {0};
    char *bootFile = NULL;
    char *appFile  = NULL;

    FILE *fpBoot = NULL;
    FILE *fpApp = NULL;
    FILE *fpObject = NULL;

    /* 判断输入参数是否合法 */
    if (argc != 3)
    {
        printf("param error, eg: xx.out [boot] [app]\n");
        return 0;
    }

    bootFile = argv[1];
    appFile = argv[2];

    strcpy(objectFileName, "./DoorLockMerge.bin");

    fpBoot = fopen(bootFile, "rb");
    fpApp = fopen(appFile, "rb");
    fpObject = fopen(objectFileName, "wb+");

    if (fpBoot == NULL || fpBoot == NULL || fpObject == NULL)
    {
        printf("open file error\n");
        return 0;
    }

    printf("== read boot file ==\n");

    readLen = fread(tmpBuffer, 1, sizeof(tmpBuffer), fpBoot);
    printf("boot file len: %u\n", readLen);
    printf("\n");

    printf("== write into object file ==\n");

    printf("write boot file\n");
    fwrite(tmpBuffer, 1, sizeof(tmpBuffer), fpObject);
    objectLen += sizeof(tmpBuffer);
    printf("write size: %lu\n", sizeof(tmpBuffer));
    printf("\n");

    printf("write app file\n");
    while (1)
    {
        readLen = fread(tmpBuffer, 1, sizeof(tmpBuffer), fpApp);

        if (readLen > 0)
        {
            printf("write size: %u\n", readLen);
            fwrite(tmpBuffer, 1, readLen, fpObject);
            objectLen += readLen;
        }
        else
        {
            break;
        }
    }
    printf("\n");

    printf("== complete ==\n");
    printf("object len: %u\n", objectLen);
    fclose(fpBoot);
    fclose(fpApp);
    fclose(fpObject);
    printf("\n");

    return 0;
}

